package com.cium.aqie.begin

/**
 * @Function: 计算器类
 * @Author: aqie
 * @Date: 2019/8/30 14:37
 */
class Operator(op: String) {
    val opFun: (left: Double, right: Double) -> Double

    init {
        opFun = when (op) {
            "+" -> { l, r -> l + r }
            "-" -> { l, r -> l - r }
            "*" -> { l, r -> l * r }
            "/" -> { l, r -> l / r }
            "%" -> { l, r -> l % r }
            else -> {
                throw UnsupportedOperationException(op)
            }
        }
    }

    // 运算符重载
    operator fun invoke(left: Double, right: Double): Double {
        return opFun(left, right)
    }

}